package com.huifer.wgx.service.impl;

import com.huifer.wgx.model.FileEntity;
import com.huifer.wgx.model.RtkPointEntity;
import com.huifer.wgx.pojo.CsvRtk;
import com.huifer.wgx.repository.FileRepository;
import com.huifer.wgx.repository.RtkPointRepository;
import com.huifer.wgx.service.RtkService;
import com.huifer.wgx.spec.CommonSpec;
import com.huifer.wgx.utlis.CsvUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.criteria.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

/**
 * <p>Title : RtkServiceImpl </p>
 * <p>Description : </p>
 *
 * @author huifer
 * @date 2019-03-20
 */
@Service
public class RtkServiceImpl implements RtkService {

    @Autowired
    private FileRepository fileRepository;
    @Autowired
    private RtkPointRepository rtkPointRepository;

    /**
     * 查询具体的csv文件 构造 RtkPointEntity
     *
     * @param personnelId 上传id
     * @param workid 工程id
     * @param filename 文件名
     * @return RtkPointEntity
     */
    @Override
    public List<RtkPointEntity> createList(Long personnelId, Long workid, String filename) {

        try {
        FileEntity fileEntity = CommonSpec.fileSpec(fileRepository, personnelId, workid, filename);
            Iterator<CsvRtk> csvRtkIterator = CsvUtil.csvRtkReader(fileEntity.getcPath());

            List<RtkPointEntity> rs = new ArrayList<>();
            while (csvRtkIterator.hasNext()) {
                CsvRtk next = csvRtkIterator.next();
                RtkPointEntity rtk = new RtkPointEntity();
                // 设置本体属性
                rtk.setRtkName(next.getName());
                rtk.setRtkX(next.getX());
                rtk.setRtkY(next.getY());
                rtk.setRtkZ(next.getZ());
                // 设置关联关系
                rtk.setWorkId(workid);
                rtk.setPersonnelId(personnelId);
                rtk.setFileId(fileEntity.getFileId());
                rs.add(rtk);
            }

            return rs;
        } catch (Exception e) {
            throw new RuntimeException("csv 解析异常");
        }

    }

    /**
     * 保存rtk
     *
     * @param rtkPointEntities rtk
     * @return rtk
     */
    @Override
    public List<RtkPointEntity> saveRtkList(List<RtkPointEntity> rtkPointEntities) {
        try {
            return rtkPointRepository.saveAll(rtkPointEntities);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据工程id查询rtk列表
     *
     * @param workId 工程id
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return rkt列表
     */
    @Override
    public Page<RtkPointEntity> findByWorkId(Long workId, Integer pageNum, Integer pageSize) {
        Specification<RtkPointEntity> specV = (Specification<RtkPointEntity>) (root, criteriaQuery, criteriaBuilder) -> {
            Predicate p1 = criteriaBuilder
                    .equal(root.get("workId"), workId);
            return p1;
        };
        return getRtkPointEntities(pageNum, pageSize, specV);
    }

    /**
     * rtk分页结果
     *
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @param specV 过滤条件
     * @return rtk分页结果
     */
    private Page<RtkPointEntity> getRtkPointEntities(Integer pageNum, Integer pageSize,
            Specification<RtkPointEntity> specV) {
        Sort sort = new Sort(Direction.DESC, "rtkId");
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
        return rtkPointRepository.findAll(specV, pageable);
    }

    /**
     * 根据rtk采集人id查询rtk列表
     *
     * @param personnelId rtk采集人id
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return rkt列表
     */
    @Override
    public Page<RtkPointEntity> findByPersonnelId(Long personnelId, Integer pageNum,
            Integer pageSize) {
        Specification<RtkPointEntity> specV = (Specification<RtkPointEntity>) (root, criteriaQuery, criteriaBuilder) -> {
            Predicate p1 = criteriaBuilder
                    .equal(root.get("personnelId"), personnelId);

            return p1;
        };
        return getRtkPointEntities(pageNum, pageSize, specV);

    }

    /**
     * 查询所有
     *
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return rkt列表
     */
    @Override
    public Page<RtkPointEntity> findAll(Integer pageNum, Integer pageSize) {
        return getRtkPointEntities(pageNum, pageSize, null);
    }
}
